package com.example.gokuplayalong.bases.alipay.directpay;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.sdk.app.PayTask;
import com.example.gokuplayalong.MainActivity;
import com.example.gokuplayalong.bases.alipay.directpayinterface.DirectPayInterface;
import com.example.gokuplayalong.bases.beans.AppParams;
import com.example.gokuplayalong.bases.beans.Beans;
import com.example.gokuplayalong.bases.exception.ExceptionUtils;
import com.example.gokuplayalong.bases.network.NetWorkInterface;
import com.example.gokuplayalong.bases.threadpool.CustomThreadPoolExecutor;
import com.example.gokuplayalong.bases.tools.Logger;

import java.util.Map;
import java.util.Objects;

public class DirectPay {

    public DirectPayInterface directPayInterface;

    public static DirectPay getNewDirectPay() {
        return new DirectPay();
    }

    public void payV2(String orderInfo, Beans beans) {
        CustomThreadPoolExecutor.getInstance().execute(new Runnable() {
            @Override
            public void run() {
                try {
                    PayTask alipay = new PayTask(MainActivity.getInstance());
                    Map<String, String> result = alipay.payV2(orderInfo, true);
                    Logger.d(result.toString());
                    if (Objects.equals(result.getOrDefault("resultStatus", String.valueOf(0)), "9000")) {
                        JSONObject resultj = JSON.parseObject(JSON.parseObject(result.get("result"))
                                .getString("alipay_trade_app_pay_response"));
                        if (resultj != null && !resultj.isEmpty()) {
                            if (Objects.equals(resultj.getString("msg"), "Success")) {
                                directPayInterface.success(resultj.toJSONString());
                            }
                            else {
                                directPayInterface.failed(resultj.getString("msg"));
                            }
                        }
                        else {
                            directPayInterface.failed("支付失败");
                        }
                    }
                    else {
                        directPayInterface.failed("支付失败");
                    }


                } catch (Throwable e) {
                    ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
                }
            }
        });
    }


    private void alipayHandler(Beans in) {
        try {
            Beans beansIn = new Beans();
            beansIn.getJsonObject()
                    .put("phone", AppParams.getInstance().appCurrentUser.getJsonObject()
                            .getString("phone"));
            beansIn.getJsonObject().putAll(in.getJsonObject());
            AppParams.getNewNetWork()
                    .setInterface(new NetWorkInterface() {
                        @Override
                        public int success(String msg) {

                            return 0;
                        }

                        @Override
                        public int failed(String msg) {

                            return 0;
                        }
                    })
                    .execute(AppParams.getInstance().baseUrl + "/alipayHandler", beansIn.getJsonObject()
                            .toJSONString());

        } catch (Throwable e) {
            ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
        }
    }

    public DirectPay setDirectPayInterface(DirectPayInterface directPayInterface) {
        this.directPayInterface = directPayInterface;
        return this;
    }

}
